from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, DateTime, Float, ForeignKey, JSON
from sqlalchemy.orm import relationship

from backend.app.database.session import Base


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    full_name = Column(String)
    role = Column(String, default="user")
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    requirements = relationship("Requirement", back_populates="user")
    test_cases = relationship("TestCase", back_populates="user")
    activities = relationship("Activity", back_populates="user")

class Requirement(Base):
    __tablename__ = "requirements"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, index=True)
    original_content = Column(Text)
    optimized_content = Column(Text)
    user_id = Column(Integer, ForeignKey("users.id"))
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    processing_time = Column(Float)  # 处理时间（秒）
    model_used = Column(String)  # 添加 model_used 字段

    user = relationship("User", back_populates="requirements")
    test_cases = relationship("TestCase", back_populates="requirement")


class TestCase(Base):
    __tablename__ = "test_cases"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, index=True)
    requirement_id = Column(Integer, ForeignKey("requirements.id"))
    user_id = Column(Integer, ForeignKey("users.id"))
    content = Column(Text)  # 存储JSON格式的测试用例
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    processing_time = Column(Float)  # 处理时间（秒）
    # model_used = Column(String)  # 添加 model_used 字段
    user = relationship("User", back_populates="test_cases")
    requirement = relationship("Requirement", back_populates="test_cases")


class Activity(Base):
    __tablename__ = "activities"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    activity_type = Column(String, index=True)  # 'requirement_optimization', 'test_case_generation', 'settings_update'
    description = Column(String)
    created_at = Column(DateTime, default=datetime.utcnow)

    user = relationship("User", back_populates="activities")


class Statistic(Base):
    __tablename__ = "statistics"

    id = Column(Integer, primary_key=True, index=True)
    date = Column(DateTime, index=True)
    requirements_count = Column(Integer, default=0)
    test_cases_count = Column(Integer, default=0)
    avg_processing_time = Column(Float, default=0)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)


# 如果确实需要 DashboardMetrics 类，可以添加如下定义
class DashboardMetrics(Base):
    __tablename__ = "dashboard_metrics"

    id = Column(Integer, primary_key=True, index=True)
    date = Column(DateTime, index=True, default=datetime.utcnow)
    metrics = Column(JSON)  # 存储仪表盘指标的JSON数据
    created_at = Column(DateTime, default=datetime.utcnow)